<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="4">
        <el-input
          v-model="queryForm.name"
          clearable
          placeholder="请输入姓名搜索"
        />
      </el-col>
      <el-button type="primary" :icon="Search" @click="initSupervisor"
        >搜索</el-button
      >
      <el-button type="primary" @click="Add">新增督导</el-button>
    </el-row>

    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column
        :width="item.width"
        :prop="item.prop"
        :label="item.label"
        v-for="(item, index) in supervisorManagement_table_recycle"
        :key="index"
        sortable
      >
        <template v-slot="{ row }" v-if="item.prop === 'gender'">
          {{ row.gender == 0 ? "男性" : "女性" }}
        </template>
        <template #default="{ row }" v-else-if="item.prop === 'action'">
          <el-button type="primary" @click="Modify(row)">修改</el-button>
          <el-button type="danger" @click="Delete(row)">删除</el-button>
        </template> </el-table-column
      >>
    </el-table>
    <el-pagination
      v-model:currentPage="queryForm.page"
      v-model:page-size="queryForm.size"
      :page-sizes="[1, 2, 5, 10]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>

  <el-dialog v-model="dialogAddVisible" title="新增督导">
    <el-form :model="form">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="queryAdd.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input v-model="queryAdd.password" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialogAdd-footer">
        <el-button @click="AddClose">取消</el-button>
        <el-button type="primary" @click="AddConfirm(queryAdd)">确认</el-button>
      </span>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogModifyVisible" title="修改督导">
    <el-form :model="form">
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="queryModify.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="性别">
        <div>
          <el-radio v-model="queryModify.gender" :label="0">男性</el-radio>
          <el-radio v-model="queryModify.gender" :label="1">女性</el-radio>
        </div>
      </el-form-item>
      <!--  -->
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="queryModify.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input
          v-model="tmp_password"
          placeholder="不输入内容则为原密码"
          autocomplete="off"
        />
      </el-form-item>
      <!--  -->
      <el-form-item label="年龄" :label-width="formLabelWidth">
        <el-input v-model="queryModify.age" autocomplete="off" />
      </el-form-item>
      <el-form-item label="身份证号" :label-width="formLabelWidth">
        <el-input v-model="queryModify.identityNumber" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="queryModify.phoneNumber" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="queryModify.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="职称" :label-width="formLabelWidth">
        <el-input v-model="queryModify.title" autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门" :label-width="formLabelWidth">
        <el-input v-model="queryModify.department" autocomplete="off" />
      </el-form-item>
      <el-form-item label="资质" :label-width="formLabelWidth">
        <el-input v-model="queryModify.qualification" autocomplete="off" />
      </el-form-item>
      <el-form-item label="最大咨询数" :label-width="formLabelWidth">
        <el-input v-model="queryModify.maxConsults" autocomplete="off" />
      </el-form-item>
      <!-- <el-form-item label="自述" :label-width="formLabelWidth">
        <el-input v-model="queryModify.introduction" autocomplete="off" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <span class="dialogModify-footer">
        <el-button @click="ModifyClose">取消</el-button>
        <el-button type="primary" @click="ModifyConfirm()">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import md5 from "js-md5";
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Search } from "@element-plus/icons-vue";
import {
  addSupervisor,
  getSupervisorList,
  deleteUser,
  modifyUser,
  getOnlineTable_supervisor,
  getCurrentUserData,
} from "@/service/api_admin";
import { supervisorManagement_table_recycle } from "./options";
const queryForm = ref({
  page: 1,
  size: 10,
  role: 2,
  name: "",
});
const form = ref({
  name: "",
  date: 0,
});

const dialogAddVisible = ref(false);
const dialogModifyVisible = ref(false);
const total = ref(0);
const tableData = ref([]);
const username_id = ref([{}]);

const initSupervisorList = async () => {
  //重新定义一个queryForm
  const tmp = {
    page: queryForm.value.page - 1,
    size: queryForm.value.size,
    role: 2,
    name: queryForm.value.name,
  };
  const res = await getSupervisorList(tmp);
  tableData.value = res.data.result.counsellorList;
  total.value = res.data.result.totalCount;
  console.log("------");
  console.log(res.data.result);
};

initSupervisorList();

//dialog相关按钮
//新增咨询师
//新增咨询师按钮，点击打开新增咨询师dialog
const queryAdd = ref({
  username: "",
  password: "",
  role: 2,
});
const Add = () => {
  dialogAddVisible.value = true;
};
//关闭新增咨询师dialog
const AddClose = () => {
  dialogAddVisible.value = false;
};

//确认添加
const AddConfirm = async () => {
  const tmp = {
    username: queryAdd.value.username,
    password: md5(queryAdd.value.password),
    role: queryForm.value.role,
  };
  console.log(tmp);
  const msg_res = await addSupervisor(tmp);
  dialogAddVisible.value = false;
  if (msg_res.data.code == 0) {
    ElMessage({
      type: "success",
      message: "添加成功",
    });
    init_username_id();
    initSupervisorList();
  } else {
    ElMessage({
      type: "warning",
      message: msg_res.data.message,
    });
  }
};
//修改
const queryModify = ref({
  username: "",
  name: "",
  password: "",
  status: 0,
  gender: 0,
  age: 0,
  identityNumber: "",
  phoneNumber: "",
  //头像url
  avatar: "",
  email: "",
  //职称
  title: "",
  department: "",
  //资质
  qualification: "",
  //自述
  introduction: "无",
  //最大咨询数
  maxConsults: 1,
});
const tmp_password = ref("");

//根据用户名获取id
const init_username_id = async () => {
  const res = await getOnlineTable_supervisor();
  username_id.value = res.data.result.counsellorList;
};

init_username_id();

const Modify = async (row) => {
  queryModify.value.username = row.username;
  const list = username_id.value;
  for (let i = 0; i < list.length; ++i) {
    //找到username对应id
    if (row.username == list[i].username) {
      // console.log(row.username+":"+list[i].counsellorID)
      const get_msg = await getCurrentUserData(list[i].counsellorID);
      const data = get_msg.data.result;
      queryModify.value = data;
      console.log(queryModify.value);
      break;
    }
  }
  dialogModifyVisible.value = true;
};
const ModifyConfirm = async () => {
  if (tmp_password.value != "") {
    queryModify.value.password = md5(tmp_password.value);
  }
  queryModify.value.age = parseInt(queryModify.value.age);
  queryModify.value.maxConsults = parseInt(queryModify.value.maxConsults);
  await modifyUser(queryModify.value);
  dialogModifyVisible.value = false;
  initSupervisorList();
};
const ModifyClose = () => {
  dialogModifyVisible.value = false;
};

//删除
const Delete = (row) => {
  ElMessageBox.confirm("将要删除用户，确认继续？", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const tmp = {
        page: queryForm.value.page - 1,
        size: queryForm.value.size,
        role: queryForm.value.role,
      };
      const res = await getSupervisorList(tmp);
      const list = res.data.result.counsellorList;
      for (let i = 0; i < list.length; ++i) {
        if (list[i].username == row.username) {
          const msg_res = await deleteUser(list[i].counsellorID);
          if (msg_res.data.code == 0) {
            ElMessage({
              type: "success",
              message: "该用户已被删除",
            });
            initSupervisorList();
          } else {
            ElMessage({
              type: "warning",
              message: "网络状况异常",
            });
          }
        }
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除操作已取消",
      });
    });
};

//分页器相关
const handleSizeChange = (pageSize) => {
  queryForm.value.page = 1;
  queryForm.value.size = pageSize;
  initSupervisorList();
};

const handleCurrentChange = (pageNum) => {
  queryForm.value.page = pageNum;
  initSupervisorList();
};
</script>

<style lang="less">
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  margin-top: 10px;
}
</style>